﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProductionManagement.Application.Command.MaterialCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProductionManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class MaterialController : ControllerBase
    {
        protected readonly IMediator mediatorp;
        public MaterialController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        ///物料添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> MaterialAdd(MaterialCeaterCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.AddDtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.AddDtos.AddName = name;
                cd.AddDtos.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.AddDtos.User_Id = int.Parse(userId);

                result.Code = 200;
                result.Message = "添加成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }



        /// <summary>
        ///物料显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<MaterialBlikDto>>> MaterialShow([FromQuery]MaterialShowCommand cd)
        {
            ApiResult<PageDTO<MaterialBlikDto>> result = new ApiResult<PageDTO<MaterialBlikDto>>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.Office_Id = sid;

                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }


        /// <summary>
        ///物料删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> MaterialDel(MaterialDelCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;

                result.Code = 200;
                result.Message = "删除成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }

        /// <summary>
        ///物料反填
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialBlikDto>>> MaterialBlik([FromQuery] MaterialBlikCommand cd)
        {
            ApiResult<List<MaterialBlikDto>> result = new ApiResult<List<MaterialBlikDto>>();
            try
            {
               

                result.Code = 200;
                result.Message = "显示成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }

        /// <summary>
        ///物料修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> MaterialUpd( MaterialUpdCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.UpdDtos.UpdName = name;
                cd.UpdDtos.UpdTime = DateTime.Now;
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }



    }
}
